package com.zds.passwordbox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zds.passwordbox.R;
import com.zds.passwordbox.common.util.AccountIconUtil;
import com.zds.passwordbox.data.dao.domain.UserAccount;

import java.util.LinkedList;

public class NormalAccountAdapter extends BaseAdapter {

    LinkedList<UserAccount> dataList;
    private final Context mContext;

    public NormalAccountAdapter(LinkedList<UserAccount> dataList, Context mContext) {
        this.dataList = dataList;
        this.mContext = mContext;
    }


    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return dataList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false);
        }
        ImageView img_icon = (ImageView) convertView.findViewById(R.id.account_icon);
        TextView accountType = (TextView) convertView.findViewById(R.id.account_type);
        TextView accountUsername = (TextView) convertView.findViewById(R.id.account_number);
        Integer iconR = AccountIconUtil.getIconR(dataList.get(position).getTypeText());
        img_icon.setBackgroundResource(iconR == null ? R.mipmap.default_account : iconR);
        accountType.setText(dataList.get(position).getTypeText());
        accountUsername.setText(dataList.get(position).getUsername());
        return convertView;
    }
}
